package com.imooc.controller;

import com.imooc.pojo.Carousel;
import com.imooc.pojo.Category;
import com.imooc.pojo.vo.CategoryVO;
import com.imooc.service.BannerService;
import com.imooc.service.CategoryService;
import com.imooc.utils.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

;
import java.util.List;

/**
 * Created by Administrator on 2020/6/5.
 */

@Api(value = "首页",tags = "首页展示接口")
@RestController
@RequestMapping("index")
public class IndexController {

    @Autowired
    private BannerService bannerService;

    @Autowired
    private CategoryService categoryService;

    @ApiOperation(value = "获取首页轮播列表",notes = "获取首页轮播列表",httpMethod = "GET")
    @GetMapping("/carousel")
    public JsonResult getBanner(){
       List<Carousel> list = bannerService.queryAll(1);
        return JsonResult.ok(list);
    }


    /**
     *  首页分类:
     *  1.初始化刷新查询大分类，渲染展示
     * 2.鼠标位移大分类，加载子分类，若存在子分类，不加载
     */
    @ApiOperation(value = "获取首页分类列表",notes = "获取首页分类列表",httpMethod = "GET")
    @GetMapping("/cats")
    public JsonResult queryAllRootLevel(){
        List<Category> list = categoryService.queryAllRootLevel();
        return JsonResult.ok(list);
    }

    @ApiOperation(value = "获取首页商品分类列表",notes = "获取首页商品分类列表",httpMethod = "GET")
    @GetMapping("/subCat/{rootCatId}")
    public JsonResult queryAllTwoLevel(
            @ApiParam(name = "rootCatId",value = "一级分类id",required = true)
            @PathVariable Integer rootCatId){
        if (null == rootCatId){
            return JsonResult.errorMsg("分类不存在");
        }
        List<CategoryVO> list = categoryService.getSubCatList(rootCatId);
        return JsonResult.ok(list);
    }


}
